package volume;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import log.LogFile;
import drive.DriveLocations;

public class Main {

	private static final File INPUT_DIR = new File(DriveLocations.CLYDE
			+ "/Music/Temp");
	private static final File OK_DB_DIR = new File(DriveLocations.CLYDE
			+ "/Music/Util/98DB DONE");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-YYYY HH:mm");
		println("SetVolumeToAll STARTED on " + format.format(date));
		println();
		new Main().run(args);
		println("DONE.");
	}

	private void run(String[] args) {

		File[] mp3s = getMP3List();
		
		for (int i = 0; i < mp3s.length; i++) {
			File mp3 = mp3s[i];
			
			try {
				Process p = Runtime
				.getRuntime()
				.exec(new String[] {
						"E:/Documents/_bin/mp3gain-win-full-1_2_5/mp3gain.exe",
						"/c", "/p", "/r", "/d", "9.0",
						"\"" + mp3.getPath() + "\"" });
				
				p.waitFor();
				println("MP3 volume has been adjusted.");
//			displayProcessResult(p);
				
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
			
			
			mp3.renameTo(new File(OK_DB_DIR, mp3.getName()));
			println("MP3 moved to \"98 DB ready\" folder.");
			println();
		}
		
	}

	private File[] getMP3List() {
		return INPUT_DIR.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File file, String name) {
				return name.endsWith(".mp3");
			}
		});
	}

	private static void println() {
		println("");
	}

	private static void println(String message) {
		System.out.println(message);
		LogFile.logln(message);
	}
}
